<?php
namespace app\admin\controller;
use app\admin\logic\ModularDictLogic;

/**
 * 模块字典api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/24
 * Time: 14:30
 */
class ModularDict extends Base {

    public function __construct(){
        parent::__construct();
        $this->md_logic = new ModularDictLogic();
    }

    /**
     * 获取模块字典
     * @return \think\response\Json
     */
    public function ajax_get_modular_dict(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->md_logic->get_search_modular_dict_maps();
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('modular_dict')->get_list($maps,'id,name,sort','sort asc','',$limit,$params['rows']);

        $total_num = model('modular_dict')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->md_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增模块字典
     * @return \think\response\Json
     */
    public function ajax_add_modular_dict(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增模块字典规则
        $r = $this->md_logic->check_add_modular_dict_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $m_data = [];
        $m_data['name'] = trim($params['name']);
        $r = model('modular_dict')->insert($m_data);
        if($r){
            return json($this->md_logic->get_response(1,'新增成功'));
        }
        return json($this->md_logic->get_response(0,'新增失败'));
    }

    /**
     * 修改模块字典
     * @return \think\response\Json
     */
    public function ajax_save_modular_dict(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改区域字典规则
        $r = $this->md_logic->check_save_modular_dict_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $md_detail = $r['data']['md_detail'];

        $m_data = [];
        $m_data['name'] = trim($params['name']);

        $maps = [];
        $maps[] = ['id','=',$md_detail['id']];
        $r = model('modular_dict')->force()->save($m_data,$maps);
        if($r !== false){
            return json($this->md_logic->get_response(1,'修改成功'));
        }
        return json($this->md_logic->get_response(0,'修改失败'));
    }

    /**
     * 删除模块字典
     * @return \think\response\Json
     */
    public function ajax_del_modular_dict(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $r = model('modular_dict')->destroy($params['mdid']);
        if($r){
            return json($this->md_logic->get_response(1,'删除成功'));
        }
        return json($this->md_logic->get_response(0,'删除失败'));
    }

    /**
     * 获取公用模块字典
     * @return \think\response\Json
     */
    public function ajax_get_public_modular_dict(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->md_logic->get_search_public_modular_dict_maps();

        $list = model('modular_dict')->get_list($maps,'id,name,sort','sort asc');

        $data['list'] = $list;
        return json($this->md_logic->get_response(1,'OK',$data));
    }
}